;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.PurchaseDeliveryReport", [])
        .controller("PurchaseDeliveryReportCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {
            $scope.markInfo = {
                allChecked: false,
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.toolbarConfig = {
                showColConfig: false
            };

            $scope.datePickerFlag = [false, false, false, false,false, false, false, false,false, false];
            $scope.condition = {};
            $scope.dataList = [];
            $scope.tbConf = {
                scrollWidth: 1070,
                tableCol: [
                    {
                        type: "",
                        name: "采购合同号",
                        dataField: "purchaseContractNo",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "供货商",
                        dataField: "supplierOrgName",
                        width: 170,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "国家和地区",
                        dataField: "country",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "进出口销售业务部门",
                        dataField: "saleDeptName",
                        width: 170,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },

                    {
                        type: "",
                        name: "进出口销售人员姓名",
                        dataField: "saleName",
                        width: 170,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "进出口采购人员姓名",
                        dataField: "purchaseName",
                        width: 170,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "是否前移",
                        dataField: "forwardShow",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "采购总金额（元）",
                        dataField: "settlementValue",
                        width: 170,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "工厂已发金额（元）",
                        dataField: "shippedValue",
                        width: 170,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "工厂未发金额（元）",
                        dataField: "noShippedValue",
                        width: 170,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "进出口已发金额（元）",
                        dataField: "jckShippedValue",
                        width: 170,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "进出口未发金额（元）",
                        dataField: "jckNoShippedValue",
                        width: 170,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "进出口销售订单确认下发时间",
                        dataField: "saleConfirmDateShow",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "进出口采购订单确认下发时间",
                        dataField: "purchaseConfirmDateShow",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "工厂订单审核时间",
                        dataField: "purchaseAuditDateShow",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "进出口运单创建时间",
                        dataField: "jckCreateDateShow",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "订单采购总数量",
                        dataField: "orderPurchaseAmount",
                        width: 170,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "工厂发运总数量",
                        dataField: "deliveryAmount",
                        width: 170,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "工厂装箱总数量",
                        dataField: "boxupAmount",
                        width: 170,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "进出口发运总数量",
                        dataField: "jckDeliveryAmount",
                        width: 170,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "工厂发运状态",
                        dataField: "deliveryStatusShow",
                        width: 170,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "工厂发运完成时间",
                        dataField: "deliveryDateShow",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "进出口发运完成状态",
                        dataField: "jckDeliveryStatusShow",
                        width: 170,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "进出口发运完成时间",
                        dataField: "jckDeliveryDateShow",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },

                ]
            };


            var btnPermissionCode = {
                purchaseDeliveryExport: window.ActionCode.purchaseDeliveryExport
            };

            $scope.btnPermission = {
                purchaseDeliveryExport: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {

                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            $scope.switchDataPicker = function (index) {
                $scope.datePickerFlag = [false, false, false, false,false, false, false, false,false, false];
                if (angular.isDefined(index)) {
                    $scope.datePickerFlag[index] = true;
                }
            };

            $scope.exportParam = {
                sectionUrl: ybHttp.RequestUrl("device", "/purchaseRequest/exportPurchaseDeliveryReport"),
                sectionParam: {ids: []},
                allUrl: ybHttp.RequestUrl("device", "/purchaseRequest/exportPurchaseDeliveryReportAll")
            };
            $scope.reset = function () {
                $scope.condition = {};
            };

            function getDataList(curPage) {
                $scope.markInfo.allChecked = false;
                $scope.markInfo.curRow = -1;
                $scope.exportParam.sectionParam.ids = [];
                var service = "device";
                var reqUrl = "/purchaseRequest/purchaseDeliveryReport";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }

                    angular.forEach(resp.value.list, function (item) {
                        item.saleConfirmDateShow = $filter("YbDate")(item.saleConfirmDate, "yyyy-MM-dd HH:mm:ss");
                        item.purchaseConfirmDateShow = $filter("YbDate")(item.purchaseConfirmDate, "yyyy-MM-dd HH:mm:ss");
                        item.purchaseAuditDateShow = $filter("YbDate")(item.purchaseAuditDate, "yyyy-MM-dd HH:mm:ss");
                        item.jckCreateDateShow = $filter("YbDate")(item.jckCreateDate, "yyyy-MM-dd HH:mm:ss");
                        item.deliveryDateShow = $filter("YbDate")(item.deliveryDate, "yyyy-MM-dd HH:mm:ss");
                        item.jckDeliveryDateShow = $filter("YbDate")(item.jckDeliveryDate, "yyyy-MM-dd HH:mm:ss");
                        item.forwardShow = $filter("TrueOrFalseOrNull")(item.forward);
                        item.deliveryStatusShow = $filter("ProgressStatus")(item.deliveryStatus, "purchaseDeliveryStatus");
                        item.jckDeliveryStatusShow = $filter("ProgressStatus")(item.jckDeliveryStatus, "purchaseDeliveryStatus");
                        item.isChecked = false;
                    });

                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };

            $scope.search = function (curPage) {
                getDataList(curPage);
            };

            function getBillIds() {
                $scope.exportParam.sectionParam.ids = [];
                angular.forEach($scope.dataList, function (item) {
                    if (!!item.isChecked) {
                        $scope.exportParam.sectionParam.ids.push(item.billId);
                    }
                });
            }

            $scope.selectAll = function (list, type) {
                for (var i = 0, len = list.length; i < len; i++) {
                    list[i].isChecked = angular.isUndefined(type) ? $scope.markInfo.allChecked : $scope.markInfo["allChecked" + type];
                }
                getBillIds();
            };

            function checkAll(list) {
                var selectAll = false;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            $scope.selectSingle = function (item, list, type) {
                var selectAll = false;
                if (!item.isChecked) {
                    selectAll = false;
                } else {
                    if (!checkAll(list)) {
                        selectAll = false;
                    } else {
                        selectAll = true;
                    }
                }
                if (angular.isDefined(type)) {
                    $scope.markInfo["allChecked" + type] = selectAll;
                } else {
                    $scope.markInfo.allChecked = selectAll;
                }
                getBillIds();
            };


            (function () {
                //getDataList();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);
